package com.example.demo.controllers;

import com.example.demo.form.ActionForm;
import com.example.demo.form.UserToRoomForm;
import com.example.demo.pojo.Room;
import com.example.demo.pojo.User;
import com.example.demo.service.GameService;
import com.example.demo.service.UserService;
import com.example.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author luolinila
 */
@RestController
public class GameControllers {

    @Autowired
    private GameService gameService;
    @Autowired
    private UserService userService;

    //创建房间
    @PostMapping(value = "/room/create")
    public Result<Room> createRoom() {
        int id = gameService.createGame();
        System.out.println(id);
        Room room = gameService.getRoom(id);
        System.out.println(room);
        return new Result<>(200, "创建房间", room);
    }

    //向房间加入用户
    @PostMapping(value = "/room/add")
    public Result<Room> addRoom(@RequestBody UserToRoomForm form) {
        User user = userService.getUserById(form.getUserId());
        Room room = gameService.getRoom(form.getRoomId());
        if (room == null || user == null) {
            return new Result<>(10004, "用户或房间不存在", null);
        }
        room = gameService.addUser(room, user);
        if (room == null) {
            return new Result<>(10005, "房间已满或房间内对局已开始", null);
        }
        return new Result<>(200, "加入成功", room);
    }

    //移除房间用户
    @PostMapping(value = "/room/remove")
    public Result<Room> removeRoom(@RequestBody UserToRoomForm form) {
        User user = userService.getUserById(form.getUserId());
        Room room = gameService.getRoom(form.getRoomId());
        if (room == null || user == null) {
            return new Result<>(10004, "用户或房间不存在", null);
        }
        room = gameService.removeUser(room, user);
        if (room == null) {
            return new Result<>(10005, "用户不在房间内或房间对局已开始", null);
        }
        return new Result<>(200, "删除成功", room);
    }

    //开始对局
    @PostMapping(value = "/room/start")
    public Result<Room> startRoom(@RequestParam("ROOM_ID") Integer roomId) {
        Room room = gameService.getRoom(roomId);
        if (room == null) {
            return new Result<>(10004, "房间不存在", null);
        }
        room = gameService.startGame(room);
        if (room == null) {
            return new Result<>(10005, "对局已经开始或未具备开局条件", null);
        }
        return new Result<>(200, "对局开始", room);
    }

    //结束对局
    @PostMapping(value = "/room/end")
    public Result<Room> endRoom(@RequestParam("ROOM_ID") Integer roomId) {
        Room room = gameService.getRoom(roomId);
        if (room == null) {
            return new Result<>(10004, "房间不存在", null);
        }
        room = gameService.endGame(room);
        if (room == null) {
            return new Result<>(10005, "对局未开始", null);
        }
        return new Result<>(200, "对局结束", room);
    }

    //玩家操作
    @PostMapping(value = "/room/action")
    public Result<Room> action(@RequestBody ActionForm form) {
        Room room = gameService.getRoom(form.getRoomId());
        User user = userService.getUserById(form.getUserId());
        if (room == null || user == null) {
            return new Result<>(10004, "用户或房间不存在", null);
        }
        room = gameService.action(room, user, form.getAction());
        if (room == null) {
            return new Result<>(10005, "操作玩家错误或操作失败", null);
        }
        return new Result<>(200, "操作成功", room);
    }

    //中止对局
    @PostMapping(value = "/room/stop")
    public Result<Room> stopRoom(@RequestParam("ROOM_ID") Integer roomId) {
        Room room = gameService.getRoom(roomId);
        if (room == null) {
            return new Result<>(10005, "房间不存在", null);
        }
        room = gameService.stop(room);
        if (room == null) {
            return new Result<>(10005, "对局未开始", null);
        }
        return new Result<>(200, "对局结束", room);
    }
}
